﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnterpriseLibraryWrapper;
using System.Data;
using itau.im.entidade;

namespace itau.im.dado
{
    public class ConfiguracoesDB : IConfiguracoesDAO
    {

        #region Métodos

        public string ObterConfiguracaoPorDominioEChave(string dominioAplicacao, string chave)
        {
            string strRetorno = string.Empty;
            DataSet dsRetorno;
            IDataAccessWrapper data = null;

            try
            {
                data = EnterpriseLibraryFactory.GetDataAccess();
                data.ParameterIn.Add(new ParameterInWrapper("@NOM_DOMN_PARM", dominioAplicacao, DbType.String));
                data.ParameterIn.Add(new ParameterInWrapper("@NOM_PARM", chave, DbType.String));
                data.ParameterOut.Add(new ParameterOutWrapper("@VLR_PARM", DbType.String, 800));
                dsRetorno = data.ExecuteParameterToDataSet("DB2IM.IMS48");

                if(dsRetorno.Tables.Count > 0 && dsRetorno.Tables[0].Rows.Count > 0)
                {
                    strRetorno = dsRetorno.Tables[0].Rows[0]["VLR_PARM"].ToString();
                }

                return strRetorno;
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosDados");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosDados");
            }
            finally
            {
                dsRetorno = null;
                data.Dispose();
                data = null;
            }

            return strRetorno = string.Empty;
        }

        public void AtualizarConfiguracao(string dominioAplicacao, string chave, string valor)
        {
            IDataAccessWrapper data = null;

            try
            {
                data = EnterpriseLibraryFactory.GetDataAccess();
                data.ParameterIn.Add(new ParameterInWrapper("@COD_DOMN_PARM", dominioAplicacao, DbType.String));
                data.ParameterIn.Add(new ParameterInWrapper("@NOM_PARM", chave, DbType.String));
                data.ParameterIn.Add(new ParameterInWrapper("@VLR_PARM", valor, DbType.String));
                data.ExecuteNonQuery("DB2IM.IMS50");
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosDados");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosDados");
            }
            finally
            {
                data.Dispose();
                data = null;
            }
        }

        public List<Item> ObterMapaConfiguracoesPorDominio(string dominio)
        {
            IDataAccessWrapper data = null;
            List<Item> configuracoes = null;
            Item item;
            DataSet ds;

            try
            {
                configuracoes = new List<Item>();
                data = EnterpriseLibraryFactory.GetDataAccess();
                
                data.ParameterIn.Add(new ParameterInWrapper("@COD_DOMN_PARM", dominio, DbType.String));
                ds = data.ExecuteDataSet("DB2IM.IMS49");
                
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow linha in ds.Tables[0].Rows)
                    {
                        item = EntidadeFactory.getMap(linha);
                        configuracoes.Add(item);
                    }
                }
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosDados");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosDados");
            }
            finally
            {
                data.Dispose();
                data = null;
                ds = null;
                item = null;
            }

            return configuracoes;
        }

        #endregion

    }
}
